package com.zoe.wan.android.example.activity.knowledge

import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import com.zoe.wan.android.example.BR
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.activity.webview.WebActivity
import com.zoe.wan.android.example.common.adapter.KnowledgeListDetailAdapter
import com.zoe.wan.android.example.databinding.FragmentDetialListBinding
import com.zoe.wan.android.example.repositry.data.knowledge.KnowledgeDetialDataData
import com.zoe.wan.base.BaseFragment

class FragDetailList(val id: String) :
    BaseFragment<FragmentDetialListBinding, FragDetailListViewModel>() {

    private val adapter = KnowledgeListDetailAdapter()
    override fun getLayoutId(): Int {
        return R.layout.fragment_detial_list
    }

    override fun getViewModelId(): Int {
        return BR.fragDetailVm
    }

    override fun initViewData() {

        binding?.recycle?.layoutManager = LinearLayoutManager(context)
        binding?.recycle?.adapter = adapter


//        viewModel?.mydata?.observe(viewLifecycleOwner) {
//            adapter.setDataList(it.datas)
//        }

        adapter?.registerListener(object : KnowledgeListDetailAdapter.KnowledgeListDetailListener {
            override fun itemClick(item: KnowledgeDetialDataData) {
                item.title?.let {
                    item?.link?.let {
                        jumpWeb(item.title, item.link)
                    }
                }

            }
        })

        viewModel?.getDetailData(id){
            adapter.setDataList(it.datas)
        }


//        binding?.

    }

    fun jumpWeb(title: String, link: String) {
        val intent = Intent(context, WebActivity::class.java)
        intent.putExtra(WebActivity.INTENT_WEB_TITLE_KEY, title)
        intent.putExtra(WebActivity.INTENT_WEB_URL_KEY, link)
        startActivity(intent)
    }
}